package com.mtl.article.service.impl;

import com.mtl.article.domain.Article;
import com.mtl.article.mapper.ArticleMapper;
import com.mtl.article.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.mtl.common.tool.model.Page;

import java.util.List;
/**
 * 文章管理 服务实现类
 *
 * @author lsc
 * @since 2019-08-09
 */
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Article findById(Integer id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Article> findPage(Integer page, Integer size,String search) {
        PageHelper.startPage(page,size);
        List<Article> articles=articleMapper.selectByCondition(search);
        return new Page<>(articles);
    }

    @Override
    public Page<Article> findBySid(Integer page,Integer size,Integer id) {
        PageHelper.startPage(page,size);
        List<Article> articles=articleMapper.selectBySortid(id);
        return new Page<>(articles);
    }

    @Override
    public int saveOrUpdate(Article article) {
        if(article.getId()!=null){
        return articleMapper.updateByPrimaryKey(article);
        }
        return articleMapper.insert(article);
    }

    @Override
    public Integer findCount() {
        return articleMapper.count();
    }

    @Override
    public Page<Article> findByImg(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Article> articles=articleMapper.selectByImg();
        return new Page<>(articles);
    }

    @Override
    public Page<Article> findOrder(Integer page, Integer size, String order, String sort,Boolean img,Integer sortid) {
        PageHelper.startPage(page,size);
        List<Article> articles=articleMapper.selectOrder(order,sort,img,sortid);
        return new Page<>(articles);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        return articleMapper.deleteByIdList(ids);
    }
}
